package manage.dao;

import manage.domain.Student;
import manage.domain.Teacher;

public class TeacherDao {
    //创建一个长度为5的老师数组
    static Teacher[] teas = new Teacher[5];

    //使用代码块初始化，让系统在每次启动时获得初始化数据
    static {
        Teacher tea = new Teacher("heima001", "张三", "23", "1999-11-11");
        Teacher tea1 = new Teacher("heima002", "李四", "24", "2000-11-11");
        teas[0] = tea;
        teas[1] = tea1;
    }

    //添加老师信息到Teacher数组中
    public boolean addTeacher(Teacher stu) {
        int index = -1;
        for (int i = 0; i < teas.length; i++) {
            Teacher teacher = teas[i];//通过遍历获取每个老师的信息
            if (teacher == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            teas[index] = stu;
            return true;
        }
    }

    //获取teacher数组中所有的老师信息
    public Teacher[] findAllTeacher() {
        return teas;//返回老师数组
    }

    //通过老师id删除对应的老师信息
    public void deleteTeacherById(String delId) {
        int index = getIndex(delId);
        teas[index] = null;//在数组中使用null替换对应的老师对象
    }

    //通过用户输入的Id值获取所对应老师信息的索引（主要用于deleteTeacherById方法和updateTeacher方法）
    public int getIndex(String delId) {
        int index = -1;
        for (int i = 0; i < teas.length; i++) {
            Teacher stu = teas[i];
            if (stu != null && stu.getId().equals(delId)) {//老师数组不为空并且Id对应用户输入的Id
                index = i;//让索引index等于对应老师信息的i值
                break;
            }
        }
        return index;
    }

    //修改老师信息
    public void updateTeacher(String updateId, Teacher newStu) {
        int index = getIndex(updateId);
        teas[index] = newStu;//使用null换掉新包装的类
    }
}
